package com.framework;

import java.util.EnumSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;

// 异步执行装饰器
class AsyncDecorator<T, R> extends ComponentDecorator<T, R> {
    private final Executor executor;
    
    public AsyncDecorator(Component<T, R> delegate, Executor executor, 
                         EnumSet<ComponentFeature> features) {
        super(delegate, features);
        this.executor = executor;
        features.add(ComponentFeature.ASYNC_EXECUTION);
    }
    
    @Override
    public ComponentOutput<R> execute(ComponentContext<T> context) {
        CompletableFuture<ComponentOutput<R>> future = CompletableFuture.supplyAsync(
            () -> delegate.execute(context), 
            executor
        );
        
        try {
            // 在实际应用中，可能需要非阻塞处理
            return future.get();
        } catch (InterruptedException | ExecutionException e) {
            return ComponentOutput.failure("ASYNC_ERROR", "异步执行失败: " + e.getMessage());
        }
    }
}